package yawning.mew.cat;

import yawning.mew.sim.SimulationEvent;

public class EventEnergyTick extends SimulationEvent {
	CatModelSimState mState;
	int mEnergy;
	int mEnergyCap;

	EventEnergyTick(CatModelSimState state, int startEnergy, int energyCap) {
		super(state.mSimEngine);
		mState = state;
		mEnergy = startEnergy;
		mEnergyCap = energyCap;
		schedule(0.0);
	}

	@Override
	public void onExpire() {
		schedule(Double.MAX_VALUE, mState.mModel.getEnergyTickInterval());
	}

	@Override
	public void onTick() {
		mEnergy++;
		mEnergyGained++;
		if (mEnergy > mEnergyCap) {
			mEnergyOverflow += mEnergy - mEnergyCap;
			mEnergy = mEnergyCap;
		}

		// This seems excessively verbose.
//		if (mState.mIsLogging) mState.mLog.log("Energy Tick: %d / %d\n", mEnergy, mEnergyCap);

		setTickInterval(mState.mModel.getEnergyTickInterval());
	}

	public void onCatShift() {
		// Enforce the energy retention cap (modified by Furor)
		if (mEnergy > mState.mModel.maxShiftEnergy) {
			mEnergyLostShifting += mEnergy - mState.mModel.maxShiftEnergy;
			mEnergy = mState.mModel.maxShiftEnergy;
		}
	}

	public void onEnergyGain(int energy) {
		mEnergy += energy;
		if (mEnergy > mEnergyCap) {
			mEnergyOverflow += mEnergy - mEnergyCap;
			mEnergy = mEnergyCap;
		}
	}

	long mEnergyOverflow;
	long mEnergyGained;
	long mEnergyLostShifting;
}